package gui;

import javax.swing.*;
import java.awt.*;

public class FlowLayoutExample extends JFrame {
    public FlowLayoutExample(){
        setTitle("FlowLayoutExample");
        setSize(400,200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //默认居中对齐
        JPanel center=new JPanel();
        center.setLayout(new FlowLayout(FlowLayout.CENTER));
        center.setBorder(BorderFactory.createTitledBorder("靠右对齐"));
        center.add(new JButton("按钮1"));
        center.add(new JButton("按钮2"));
        center.add(new JButton("按钮3"));
        //默认靠左对齐
        JPanel left=new JPanel();
        left.setLayout(new FlowLayout(FlowLayout.LEFT));
        left.setBorder(BorderFactory.createTitledBorder("靠右对齐"));
        left.add(new JButton("按钮1"));
        left.add(new JButton("按钮2"));
        left.add(new JButton("按钮3"));
        //默认居中对齐
        JPanel right=new JPanel();
        right.setLayout(new FlowLayout(FlowLayout.RIGHT));
        right.setBorder(BorderFactory.createTitledBorder("靠右对齐"));
        right.add(new JButton("按钮1"));
        right.add(new JButton("按钮2"));
        right.add(new JButton("按钮3"));
        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
        add(center);
        add(left);
        add(right);
        setVisible(true);
    }

    public static void main(String[] args) {
        new FlowLayoutExample();
    }
}
